/*
Content: Basic data preparation of events dataset
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

***************************************************************************
** UPPSALA: MONTH-YEAR-COUNTRY OF ORIGIN ARMED CONFLICTS ATTACKS ******
***************************************************************************

* LOAD ORIGINAL DATASET
use "$dataevent/uppsala/armedconflict_uppsala.dta", clear

	* Fix date variables
	cap drop month day
	gen month = substr(date_start,6,2)
	gen day = substr(date_start,9,2)

	destring year, replace
	destring month, replace
	destring day, replace

	* Generate day-month-year and month-year variables    
gen startdate = mdy(month,day,year)
format startdate %td

gen startmy = ym(year,month)
format startmy %tm

	
* Keep only necessary variables
keep id year month day startmy startdate country_id country type_of_violence  conflict_new_id 
rename type_of_violence violence_type

gen uppsala = 1
gen armedconflict = 1

lab def violence_type 1"State violence" 2"Non-state violence" 3"One-side violence"
lab val violence_type violence_type

g state_viol=0
replace state_viol=1 if violence_type==1
g nonstate_viol=0
replace nonstate_viol=1 if violence_type==2
g onsided_viol=0
replace onsided_viol=1 if violence_type==3
g statenonstate=0
replace statenonstate=1 if violence_type==1
replace statenonstate=1 if violence_type==2
	
* Label vars
lab var country "Country name"
lab var country_id "Country ID"
lab var startmy "Month-year of the event"
lab var day "Day of the event"
lab var month "Month of the event"
lab var year "Year of the event"
lab var uppsala "Indicator for any event in Uppsala Armed conflict data"
lab var armedconflict "Total armed conflict events in a day-country"
lab var violence_type "Type of violence"


* Merge with SOEP country codes
	preserve 
	use "$codes\countrycodes_soep_armedconflict.dta", clear
		bys country_id: gen unique = 1 if _n == 1
		keep if unique == 1
		drop unique
	tempfile countrycodes_soep_armed
	save `countrycodes_soep_armed'
	restore

	* Merge
	merge m:1 country_id using `countrycodes_soep_armed'
	keep if _merge == 3 
	drop _merge 

* Collapse at the daily-country of origin level
preserve
foreach v of var * {
local l`v' : variable label `v'
   if `"`l`v''"' == "" {
	local l`v' "`v'"
}
}

sort soep_corigin_id startdate violence_type 
collapse (sum) armedconflict *_viol statenonstate uppsala (firstnm) violence_type  country country_id  day month year startmy, by(soep_corigin_id startdate)

foreach v of var * {
label var `v' `"`l`v''"'
}


save "$uppsala/armedconflict_daily_uppsala_soep_final.dta", replace
restore

* Collapse at the monthly-country of origin level
preserve
foreach v of var * {
local l`v' : variable label `v'
   if `"`l`v''"' == "" {
	local l`v' "`v'"
}
}

sort soep_corigin_id startdate violence_type 
collapse (sum) armedconflict *_viol statenonstate uppsala (firstnm) violence_type  country country_id month year, by(soep_corigin_id startmy)

foreach v of var * {
label var `v' `"`l`v''"'
}


save "$uppsala/armedconflict_monthly_uppsala_soep_final.dta", replace
restore

*************************************************************
** GTD: MONTH-YEAR-COUNTRY OF ORIGIN TERRORIST ATTACKS ******
*************************************************************

* Load original GTD database
import delimited "$globalterror\globalterror_1970_2017.csv", clear  

	* Some people country of origin is an area that no longer exists (e.g., USSR, Yugoslavia)
	append using "$globalterror\terror_dd_formeryugos.dta", generate(yugo_countries) // use former yugoslavia to have backward info on countries
	append using "$globalterror\terror_dd_countriesyugo.dta", generate(countries_yugo) // use countries to compute forward events of yugoslavia
	append using "$globalterror\terror_dd_easternbloc.dta", generate(countries_eastern) // use countries to compute forward and backward events for the former eastern bloc (from post-soviet countries)
		
* Date of event 
		
	* Complete date of the event
	gen startdate = mdy(imonth,iday,iyear)	
	* Month-year of the event
	gen startmy = ym(iyear,imonth) 
	format startmy %tm 

	rename imonth month
	rename iyear year
	rename iday day 


* Indicator for each event
gen gtd = 1
clonevar terror = gtd
clonevar terror_suc = success



*  Fix country code variable 	
rename country_txt country_name 
rename country country_code 

replace country_name = "Eswatini" if inlist(country_name,"Swaziland") 
replace country_name = "Russia" if inlist(country_name,"Soviet Union") 
replace country_name = "Côte d'Ivoire" if inlist(country_name,"Ivory Coast") 
replace country_name = "Italy" if inlist(country_name,"Vatican City")


* Merge with GSOEP country codes
	preserve 
	use "$codes/countrycodes_soep_terror.dta", clear

	bys country_code: gen unique = 1 if _n == 1
	keep if unique == 1
	drop unique

	tempfile countrycodes_soep_terror

	save `countrycodes_soep_terror'

	restore

	* Merge
	merge m:1 country_code using `countrycodes_soep_terror', keep(3) 


* Label variables 
rename nkill nkill_terror
rename nwound nwound_terror

lab var startmy "Month-year of the event"
lab var day "Day of the event"
lab var month "Month of the event"
lab var year "Year of the event"
lab var startdate "Complete date of event start"
lab var gtd "Indicator for any event in the gtd data"
lab var terror "Indicator for any terror event in the gtd data"
lab var nkill_terror "Number of killed"
lab var nwound_terror "Number of wounded"
lab var country_name "Country name"
lab var country_code "Country code in gtd" 

* Collapse at the daily-country of origin level
preserve 
foreach v of var * {
local l`v' : variable label `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}

collapse (sum) terror_suc   terror nkill_terror nwound_terror  success (mean) gtd (firstnm) country_code  country_name year month day startmy, by(soep_corigin_id startdate) 
drop if startdate==.

foreach v of var * {
label var `v' `"`l`v''"'
}

save "$globalterror/final/terror_daily_gtd_soep_final.dta", replace
restore

* Collapse at the monthly-country of origin level
preserve 
foreach v of var * {
local l`v' : variable label `v'
if `"`l`v''"' == "" {
local l`v' "`v'"
}
}

collapse (sum) terror_suc   terror nkill_terror nwound_terror success (mean) gtd (firstnm) country_code  country_name year month, by(soep_corigin_id startmy) 

foreach v of var * {
label var `v' `"`l`v''"'
}

save "$globalterror/final/terror_monthly_gtd_soep_final.dta", replace
restore
	

*************************
*** ALL DAILY DATA ***
*************************

use  "$uppsala/armedconflict_daily_uppsala_soep_final.dta", clear
cap drop _merge
merge 1:1 startdate soep_corigin_id using "$globalterror/final/terror_daily_gtd_soep_final.dta"	

foreach var of varlist armedconflict state_viol nonstate_viol onsided_viol statenonstate uppsala {
replace `var'=0 if _merge==2 & 	`var'==.

}

foreach var of varlist terror terror_* *_terror success {
replace `var'=0 if _merge==1 & 	`var'==.

}

cap drop _merge

format startdate %td
drop startmy
save "$dataevent/allevents_daily_soep_final.dta", replace
		

*************************
*** ALL MONTHLY DATA ***
*************************

use  "$uppsala/armedconflict_monthly_uppsala_soep_final.dta", clear
cap drop _merge
merge 1:1 startmy soep_corigin_id using "$globalterror/final/terror_monthly_gtd_soep_final.dta"	
	
	
foreach var of varlist armedconflict state_viol nonstate_viol onsided_viol statenonstate uppsala {
replace `var'=0 if _merge==2 & 	`var'==.

}

foreach var of varlist terror terror_* *_terror success {
replace `var'=0 if _merge==1 & 	`var'==.

}
	
	
cap drop _merge
save "$dataevent/allevents_monthly_soep_final.dta", replace
	
		
		
		